<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="../js/global.js"></script>
		<script src="../js/checked.js"></script>
		 <script src="../js/xm-select.js"></script>
		
	</head>
	<body>
		<form id="formCreateUser" class="layui-form" action="" onsubmit="return false">
			<div class="layui-form-item" style="margin-top: 30px;">
				<label class="layui-form-label" ><span style="color: red;">*</span>考试名称:</label>
				<div class="layui-input-inline" style="width: 350px;">
					<input type="text" placeholder="请输入考试名称" name="examName"
						 class="layui-input" />
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label"><span style="color: red;">*</span>考试开始时间:</label>
				<div class="layui-input-inline" style="width: 350px;">
					<input type="text"  name="examStartTime"
						 class="layui-input examStartTime" />
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label"><span style="color: red;">*</span>考试结束时间:</label>
				<div class="layui-input-inline" style="width: 350px;">
					<input type="text"  name="examEndTime"
						 class="layui-input examEndTime" />
				</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label"><span style="color: red;">*</span>监考老师:</label>
				<div class="layui-input-inline" style="width: 350px;">
				     <input type="text" id="testP" name="testPaperId" hidden="hidden" />
					 <select class="us" name="invigilateTeacherId">
						 <option></option>
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label"><span style="color: red;">*</span>考试类型:</label>
				<div class="layui-input-inline" style="width: 350px;">
					
				    <select class="ex" multiple="true" name="examTypeId">
				    	 <option></option>
				    </select>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label"><span style="color: red;">*</span>班级:</label>
				<div class="layui-input-inline">
					<div id="demo1" style="width: 350px;"></div>
				</div>
				
				<!-- 用于存储选择的数据，方便后期进行数据提交操作 -->
				<input type="hidden" id="sclass" name="sclass" >

			</div>
			
			
			
			<div class="layui-btn-container" style="text-align: center;">
				<button class="layui-btn" lay-submit="" lay-filter="addUser">添加</button>
			</div>
		</form>
		<script>
			//传递试卷id
			var devsId =  parent.$("#tid").val()
			$("#testP").val(devsId);
	  
			
			layui.use(['table', 'form', 'layer', 'laydate', 'upload', 'element','xmSelect'],function(){
				var layer = layui.layer;
				var form = layui.form;
				var table = layui.table;
				var laydate = layui.laydate;
				var upload = layui.upload;
				var element = layui.element;
				var xmSelect = layui.xmSelect;
				var $ = layui.$;
				laydate.render({
					elem: '.examStartTime',
					type: 'datetime'
				});
				laydate.render({
					elem: '.examEndTime',
					type: 'datetime'
				})
				     var tagIns1;
                    $.ajax({
                          type : "get",
                          url : globalData.organizationServer+'/class/listClass',
                 //要传递到服务端的数据
                 		dataType:'json',//服务端响应数据类型
                 		xhrFields: {
                 			withCredentials: true
                 		},
                 		// beforeSend: function(request) {
                 		// 	request.setRequestHeader("token", globalData.getCurToken());
                 		// },
                             success : function(res) {
                 			 
                 			   tagData1 = res.data; // 
                 			 console.log(res);
                 			
                 			   tagIns1 = xmSelect.render({
                 			    el: '#demo1', // div的id值
                 			    toolbar: { // 工具条【‘全选’，‘清空’】
                 			      show: true, // 开启工具条
                 			      showIcon: false, // 隐藏工具条的图标
                 			    },
                 			    // tips: '选择校区', // 让默认值不是“请选择”，而是“选择校区”
                 			    filterable: true, // 开启搜索模式，默认按照name进行搜索
                 			    paging: true, // 启用分页
                 			    pageSize: 3, // 每页的数据个数
                 			    data:tagData1 , // 动态传入数据
                 			    prop: { // 也许你的数据库返回的并不是name和value, 也许你提交的时候不止name和value, 怎么办? 自定义就行
                 			      name: 'className',
                 			      value: 'classId'
                 			    },
                 			    
                 			  })
                 			  
                 
                 	}
                 	});
					
				//渲染用户表
				$.get(globalData.permissionServer + '/user/listUsers', function(res) {
					var html = "";
					for (var i = 0; i < res.data.length; i++) {
						html += "<option value=" + res.data[i].userId + ">" + res.data[i].userName + "</option>"
				
					}
					$(".us").append(html);
				
					form.render('select')
				}, "json")
				//渲染用户表
				$.get(globalData.examinationServer + '/examType/listExamTypes', function(res) {
					var html = "";
					for (var i = 0; i < res.data.length; i++) {
						html += "<option value=" + res.data[i].examTypeId + ">" + res.data[i].examTypeName + "</option>"
				
					}
					$(".ex").append(html);
				
					form.render('select')
				}, "json")
				
				//添加按钮提交监听
				
				form.on('submit(addUser)',function(obj){
				 var classIds = tagIns1.getValue('valueStr'); // 获取到对应的value值(相当于我们常传的id)
				 globalData.setClassIds(classIds);
				 alert(globalData.getClassIds());
					$.ajax({
						type : "post",
						url : globalData.examinationServer+'/exam/insertExam?classIds=' + globalData.getClassIds(),
						data:obj.field,//需要传递到服务端的数据
						dataType:'json',//服务端响应数据类型
						xhrFields: {
							withCredentials: true
						},
						// beforeSend: function(request) {
						// 	request.setRequestHeader("token", globalData.getCurToken());
						// },
						success : function(res) {
							if(res.code == 0){
									layer.msg('添加成功',{icon:1,time:2000},function(index){
										layer.close(index);
										//关闭父窗口并重载
										dkm._close();
										dkm._reload();
									});
									
								}else{
									layer.msg('添加失败',{icon:2,time:2000});
							}
						}
					});
					
					return false;
				});
				
				
			});
		</script>
	</body>
</html>
